

#include "FIFOMemoryTable.h"


FIFOMemoryTable::FIFOMemoryTable(int parNumMaxSlot, MemoryWrapper* parMemory){

	//numMaxSlot = dimension / sizeof(PubPkt);
	numMaxSlot = parNumMaxSlot;
	numUsedSlot = 0;
	memory = parMemory;
	keyCounter = 0;

	timer = new Timer();
}

FIFOMemoryTable::~FIFOMemoryTable(){

}


void FIFOMemoryTable::computeInsertion(PubPkt *pkt, int ns, float ws, float lastPercentage){

	cout << "Memory manager: FIFO table: start message insertion..." << endl;
	timer->start();
	cout << numMaxSlot << endl;
	if (numMaxSlot != -1 && numUsedSlot == numMaxSlot) {
		cout << "Memory manager: FIFO table: capacity limit reached: need to remove a message." << endl;
		std::pair<timestamp_t, key_t> candidate = queue.top();
		memory->remove(candidate);
		numUsedSlot--;
		queue.pop();
	}
	keyCounter++;
	timestamp_t eventTimestamp = pkt->getTimeStamp().getTimeVal();
	queue.push(make_pair(eventTimestamp, keyCounter));
	memory->insert(pkt, keyCounter);
	numUsedSlot++;

	timer->stop();
	cout << "Memory manager: FIFO table: insertion terminated in " << timer->getElapsedTimeInMilliSec() << " ms." << endl;
}

void FIFOMemoryTable::computeExtraction(BackSubPkt *pkt, std::set<PubPkt*> &resultSet){

	cout << "Memory manager: FIFO table: start messages extraction..." << endl;
	memory->extract(pkt->getEventType(), pkt->getConstraintList(), pkt->getConstraintsNum(), pkt->getTimeStamp().getTimeVal(), pkt->getWindow(), pkt->getOccurrence(), resultSet);
	cout << "Memory manager: FIFO table: extraction terminated." << endl;
}




